<template>
  <div :class="['login-wrapper', ['', 'login-form-right', 'login-form-left'][direction]]">
    <el-form ref="loginForm" :model="form" :rules="rules" class="login-form ele-bg-white" size="large" @keyup.enter.native="doSubmit">
      <h4>用户登录</h4>
      <el-form-item prop="username"><el-input placeholder="请输入登录账号" v-model="form.username" prefix-icon="el-icon-user" clearable /></el-form-item>
      <el-form-item prop="password"><el-input placeholder="请输入登录密码" v-model="form.password" prefix-icon="el-icon-lock" show-password /></el-form-item>
      <!-- <el-form-item prop="code">
        <div class="login-input-group">
          <el-input placeholder="请输入验证码" v-model="form.code" prefix-icon="el-icon-_vercode" clearable />
          <img v-if="captcha" :src="captcha" @click="changeCode" class="login-captcha" alt="" />
        </div>
      </el-form-item> -->
      <div class="el-form-item">
        <el-tooltip content="公共电脑请勿勾选" placement="right" effect="dark"><el-checkbox v-model="form.remember">记住密码</el-checkbox></el-tooltip>
        <el-tooltip content="请确认注销行为" placement="right" effect="dark"><a @click="doSubmitx" style="float:right">注销登录</a></el-tooltip>
      </div>
      <div class="el-form-item">
        <el-button @click="doSubmit" :loading="loading" type="primary" class="login-btn" size="large">{{ loading ? '登录中' : '登录' }}</el-button>
      </div>
    </el-form>
    <div class="login-copyright">copyright © 2020-2023 yusvn.com all rights reserved.</div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      direction: 0, // 登录框方向,0居中,1居右,2居左
      loading: false, // 加载状态
      form: { username: '', password: '', code: '', remember: false },
      rules: {
        // 表单验证规则
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
        //,code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      captcha: '',
      text: '',
      hostif: false
    };
  },
  created() {},
  mounted() {
   // this.loginSett();
   // if (this.$store.state.user.token) this.$router.push(this.$route.query.from || '/');
	//console.log(this.$route,'this.$route');
   // this.changeCode();
  },
  methods: {
    Feehost() {
      this.$http.post('/quitout', this.from).then(res => {
        if (res.data.code === 0) {
          this.$message({ type: 'success', message: '注销成功' });
        }
      });
    },
    /* 提交 */
    doSubmit() {
      if (this.form.remember == true || this.form.remember == 'true') {
        localStorage.setItem('loginuserusername', this.form.username);
        localStorage.setItem('loginuserpassword', this.form.password);
        localStorage.setItem('loginuserremember', this.form.remember);
      } else {
        localStorage.setItem('loginuserusername', '');
        localStorage.setItem('loginuserpassword', '');
        localStorage.setItem('loginuserremember', false);
      }

      this.$refs['loginForm'].validate(valid => {
        if (!valid) return false;
        //if (this.form.code.toLowerCase() !== this.text)
        //  return this.$message.error('验证码错误')
        this.loading = true;

        this.$http.post('/login', this.form).then(res => {
          //
          this.loading = false;
          if (res.data.code === 0) {
            this.$message({ type: 'success', message: '登录成功' });
            this.$store
              .dispatch('user/setToken', {
                token: res.data.access_token, //'Bearer ' +
                remember: this.form.remember
              })
              .then(() => {
				console.log(this.$route.query.from,'进入1');
                this.$router.push(this.$route.query.from || '/').catch(() => 0);
              });
          } else {
            this.$message.error(res.data.msg);
          }
        });
      });
    },
    doSubmitx() {
      if (this.form.remember == true || this.form.remember == 'true') {
        localStorage.setItem('loginuserusername', this.form.username);
        localStorage.setItem('loginuserpassword', this.form.password);
        localStorage.setItem('loginuserremember', this.form.remember);
      } else {
        localStorage.setItem('loginuserusername', '');
        localStorage.setItem('loginuserpassword', '');
        localStorage.setItem('loginuserremember', false);
      }

      this.$refs['loginForm'].validate(valid => {
        if (!valid) return false;
        // if (this.form.code.toLowerCase() !== this.text)
        //   return this.$message.error('验证码错误')
        this.loading = true;

        this.$http.post('/quitout', this.form).then(res => {
          this.loading = false;
          if (res.data.code === 0) {
            this.$message({ type: 'success', message: '注销成功' });
            this.$store
              .dispatch('user/setToken', {
                token: res.data.access_token, //'Bearer ' +
                remember: this.form.remember
              })
              .then(() => {
                this.$router.push(this.$route.query.from || '/').catch(() => 0);
              });
          } else {
            this.$message.error(res.data.msg);
          }
        });
      });
    },
    /* 更换图形验证码 */
    changeCode() {
      // 这里演示的验证码是后端返回base64格式的形式,如果后端地址直接是图片请参考忘记密码页面
      this.$http
        .get('/file/captcha')
        .then(res => {
          if (res.data.code === 0) {
            this.captcha = res.data.data;
            //this.text = res.data.text
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          this.$message.error(e.message);
        });
    },
    loginSett() {
      this.form.username = localStorage.getItem('loginuserusername');
      this.form.password = localStorage.getItem('loginuserpassword');
      if (localStorage.getItem('loginuserremember') == true || localStorage.getItem('loginuserremember') == 'true') {
        this.form.remember = true;
      } else {
        this.form.remember = false;
      }
    }
  }
};
</script>

<style scoped>
.login-wrapper {
  padding: 50px 20px;
  position: relative;
  box-sizing: border-box;
  background-image: url('~@/assets/bg-login.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  min-height: 100vh;
}

.login-wrapper:before {
  content: '';
  background-color: rgba(0, 0, 0, 0.2);
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.login-form {
  margin: 0 auto;
  width: 380px;
  max-width: 90%;
  padding: 45px 35px;
  position: relative;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.15);
  box-sizing: border-box;
  border-radius: 4px;
  z-index: 2;
  background: #ddd5d580;
}

.login-form-right .login-form {
  margin: 0 15% 0 auto;
}

.login-form-left .login-form {
  margin: 0 auto 0 15%;
}

.login-form h4 {
  text-align: center;
  margin: 0 0 25px 0;
}

.login-form > .el-form-item {
  margin-bottom: 25px;
}

.login-input-group {
  display: flex;
  align-items: center;
}

.login-input-group >>> .el-input {
  flex: 1;
}

.login-captcha {
  height: 38px;
  width: 102px;
  margin-left: 10px;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  text-align: center;
  cursor: pointer;
}

.login-captcha:hover {
  opacity: 0.75;
}

.login-btn {
  display: block;
  width: 100%;
}

.login-oauth-icon {
  color: #fff;
  padding: 5px;
  margin: 0 10px;
  font-size: 18px;
  border-radius: 50%;
  cursor: pointer;
}

.login-copyright {
  color: #eee;
  padding-top: 20px;
  text-align: center;
  position: relative;
  z-index: 1;
}

@media screen and (min-height: 550px) {
  .login-form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%);
    margin-top: -220px;
  }

  .login-form-right .login-form,
  .login-form-left .login-form {
    left: auto;
    right: 15%;
    transform: translateX(0);
    margin: -220px auto auto auto;
  }

  .login-form-left .login-form {
    right: auto;
    left: 15%;
  }

  .login-copyright {
    position: absolute;
    bottom: 20px;
    right: 0;
    left: 0;
  }
}

@media screen and (max-width: 768px) {
  .login-form-right .login-form,
  .login-form-left .login-form {
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    margin-right: auto;
  }
}
</style>
